package com.hele.sts.pub;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

public class PropGet {
	private static final Logger logger = LoggerFactory.getLogger(PropGet.class);
	public static String dataSourceName = "java:comp/env/jdbc/myoracle";
	public static String fileSeparator;
	private static Properties o_Properties;
	public static String operationSystem = "";
	private String propertiesName;

	public PropGet(String s_propertiesName) throws IOException {
		this.propertiesName = "";
		o_Properties = new Properties();
		this.propertiesName = s_propertiesName;
		InputStream o_Reader = getClass().getResourceAsStream(s_propertiesName + ".properties");
		o_Properties.load(o_Reader);
		o_Reader.close();
	}

	public static void InitData() {
		try {
			PropGet oReader = new PropGet("clonesoft");
			String webServer = (String) oReader.getProperty("webServer");
			if (webServer.equals("weblogic")) {
				dataSourceName = "jjhkDataSource";
			}
			if (webServer.equals("tomcat")) {
				// dataSourceName = "java:comp/env/jdbc/myoracle";
				dataSourceName = "dataSource";
			}
			operationSystem = (String) oReader.getProperty("operationSystem");
			if (operationSystem.equals("linux")) {
				fileSeparator = "/";
			}
			if (operationSystem.equals("windows")) {
				fileSeparator = "\\";
			}
		} catch (Exception e) {
			logger.error("Error of create input stream", e);
		}
	}

	public Object getProperty(String s_id) {
		return o_Properties.getProperty(s_id);
	}

	public void setProperty(String key, String value) throws IOException {
		String pFilename = System.getProperty("user.dir") + System.getProperty("file.separator") + "db.properties";
		pFilename = getClass().getResource("db.properties").toString();
		pFilename = pFilename.substring(6);
		Properties p = new Properties();
		FileInputStream in = new FileInputStream(pFilename);
		p.load(in);
		in.close();
		System.out.println("Error of create input stream");
		p.setProperty(key, value);
		FileOutputStream out = new FileOutputStream(pFilename);
		p.store(out, "db Configure");
		out.flush();
		out.close();
		System.out.println("Error of write input stream");
	}
}
